﻿using System.Collections.Generic;
using Models;
using Services;
using Views;
using Models.Extensions;
using System;

namespace Presenters
{
    public class EditDatabasePresenter : PresenterBase<IEditDatabaseView>
    {
        /// <summary>
        /// Initialise the EditDatabase presenter
        /// </summary>
        /// <param name="view">A view that impliments IEditDatabase</param>
        /// <param name="liveDatabase">Reference to the database model that we want to edit</param>
        public EditDatabasePresenter(IEditDatabaseView view, Database liveDatabase)
            : base(view)
        {
            // Create a copy of the database that we are editing so that our changes do no mess up the live
            // copy of the database if the user decides to cancel the edit process
            m_DatabaseToEdit = new Database(liveDatabase);
            
            // Store reference to liveDatabase for future use
            m_LiveDatabase = liveDatabase;
        }

        /// <summary>
        /// The Edit Database user interface
        /// </summary>
        public SQPlugin.IEditDatabase UserInterface
        {
            get
            {
                if (m_UserInterface == null)
                {
                    Services.ServersService serverService = new ServersService(m_DatabaseToEdit.DatabaseServer.ServerID);
                    m_UserInterface = serverService.GetEditDatabaseUI();
                }
                return m_UserInterface;
            }
        }

        /// <summary>
        /// Update the live copy of the database model with the changes made in the DatabaseToEdit copy
        /// </summary>
        public void UpdateLiveDatabase()
        {
            // Get the edited database settings from the user interface
            Database updatedDB = UserInterface.GetUpdatedDatabase();
            m_LiveDatabase.DatabaseName = updatedDB.DatabaseName;
            m_LiveDatabase.DatabaseServer = updatedDB.DatabaseServer;
        }

        /// <summary>
        /// Make sure the details entered into the view are valid.
        /// </summary>
        /// <returns>True if the details are valid</returns>
        public bool ValidateDatabase()
        {
            // Before we validate, load the connection settings from the view
            m_DatabaseToEdit = m_UserInterface.GetUpdatedDatabase();

            ServersService serverService = new ServersService(m_DatabaseToEdit.DatabaseServer.ServerID);

            string errorMsg;
            bool result = serverService.ValidateDatabase(m_DatabaseToEdit, out errorMsg);

            if (!string.IsNullOrEmpty(errorMsg))
            {
                // We have an error message to display
                ViewManager.ViewFactory.DisplayError(errorMsg, "Error connecting to database");
            }

            return result;
        }

        /// <summary>
        /// Load the database to edit into the user interface
        /// </summary>
        public void LoadData()
        {
            UserInterface.SetDatabase(m_DatabaseToEdit);
        }

        /// <summary>
        /// Reference to the live database model that we will update with our changes
        /// </summary>
        private Database m_LiveDatabase;

        /// <summary>
        /// Reference to the copy of the database that we are editing
        /// </summary>
        private Database m_DatabaseToEdit;

        /// <summary>
        /// A reference to the user interface we are using to edit the database
        /// </summary>
        private SQPlugin.IEditDatabase m_UserInterface;
    }
}
